
package com.stnet.web.enterprice.system;

import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.modules.commons.PageAction;
import com.stnet.entity.Roles;
import com.stnet.entity.RolesAuthorities;
import com.stnet.entity.RolesAuthoritiesId;

public class RolesAction extends PageAction
{

    public RolesAction()
    {
        roles = new Roles();
    }

    public List getCheckedMenu()
    {
        return checkedMenu;
    }

    public void setCheckedMenu(List checkedMenu)
    {
        this.checkedMenu = checkedMenu;
    }

    public String execute()
        throws Exception
    {
        super.pager = rolesManagerDao.findRoles(pageSize, pageNo, "-1");
        rolesList = super.pager.getResultList();
        return "success";
    }

    public String view()
        throws Exception
    {
        roles = rolesManagerDao.getRoles(id);
        return "view";
    }

    public String edit()
        throws Exception
    {
        setAuthoritiesList(authoritiesManagerDao.findAuthorities());
        setRoles(rolesManagerDao.getRoles(id));
        return "edit";
    }

    public String checkName()
        throws Exception
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String name = request.getParameter("name");
        String orgName = request.getParameter("orgName");
        name = URLDecoder.decode(name, "UTF-8");
        orgName = URLDecoder.decode(orgName, "UTF-8");
        if(rolesManagerDao.isNameUnique(name, orgName))
            return renderText("true");
        else
            return renderText("false");
    }

    public String update()throws Exception
    {
       // initRoles();
        rolesManagerDao.updateRoles(roles);
        rolesAuthoritiesManagerDao.deleteRolesAuthoritiesByRoles(roles.getId());
        com.stnet.entity.Authorities authorities;
        for(Iterator iterator = checkedMenu.iterator(); iterator.hasNext(); rolesAuthoritiesManagerDao.addRolesAuthorities(new RolesAuthorities(new RolesAuthoritiesId(roles, authorities), roles, authorities)))
        {
            String check = (String)iterator.next();
            authorities = authoritiesManagerDao.getAuthorities(Long.valueOf(check));
        }

        return "update";
    }

    public String add_page()
        throws Exception
    {
        setAuthoritiesList(authoritiesManagerDao.findAuthorities());
        return "add_page";
    }

    public String add()
        throws Exception
    {
        initRoles();
        rolesManagerDao.addRoles(roles);
        com.stnet.entity.Authorities authorities;
        for(Iterator iterator = checkedMenu.iterator(); iterator.hasNext(); rolesAuthoritiesManagerDao.addRolesAuthorities(new RolesAuthorities(new RolesAuthoritiesId(roles, authorities), roles, authorities)))
        {
            String check = (String)iterator.next();
            authorities = authoritiesManagerDao.getAuthorities(Long.valueOf(check));
        }

        return "add";
    }

    public String delete()
        throws Exception
    {
        rolesManagerDao.deleteRoles(id);
        return "delete";
    }

    public void initRoles()
    {
        roles.setName(name);
    }

    public Roles getRoles()
    {
        return roles;
    }

    public void setRoles(Roles roles)
    {
        this.roles = roles;
    }

    public List getRolesList()
    {
        return rolesList;
    }

    public void setRolesList(List rolesList)
    {
        this.rolesList = rolesList;
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public List getAuthoritiesList()
    {
        return authoritiesList;
    }

    public void setAuthoritiesList(List authoritiesList)
    {
        this.authoritiesList = authoritiesList;
    }

    private Roles roles;
    private List rolesList;
    private List authoritiesList;
    private Long id;
    private String name;
    private List checkedMenu;
}